package cn.tedu.suppermemory.service.impl;


import cn.tedu.suppermemory.ex.ServiceException;
import cn.tedu.suppermemory.mapper.TeacherMapper;
import cn.tedu.suppermemory.pojo.dto.TeacherAddDTO;
import cn.tedu.suppermemory.pojo.dto.TeacherUpdateDTO;
import cn.tedu.suppermemory.pojo.entity.Teacher;
import cn.tedu.suppermemory.pojo.vo.TeacherListItemVO;
import cn.tedu.suppermemory.pojo.vo.TeacherStandardVO;
import cn.tedu.suppermemory.service.ITeacherService;
import cn.tedu.suppermemory.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * 处理老师数据的业务实现类
 *
 * @author java@tedu.cn
 * @version 0.0.1
 */
@Slf4j
@Controller
public class TeacherServiceImpl implements ITeacherService {

    @Autowired
    private TeacherMapper teacherMapper;

    @Override
    public void addNew(TeacherAddDTO teacherAddDTO) {
        log.debug("开始处理【添加老师】的业务，参数：{}",teacherAddDTO);
        //检查老师是否已存在
        String name = teacherAddDTO.getName();
        int countByName = teacherMapper.countByName(name);
        if (countByName > 0 ){
            String message = "添加老师失败，该老师名称已被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }
        //创建Teacher对象
        Teacher teacher = new Teacher();
        BeanUtils.copyProperties(teacherAddDTO, teacher);
        // 执行插入数据：albumMapper.insert(album)
        log.debug("即将插入数据，参数：{}", teacher);
        int rows = teacherMapper.insert(teacher);
        if (rows != 1) {
            String message = "添加老师失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理【根据id删除老师】的业务，参数：{}", id);
        {
            // 调用Mapper的getStandardById()执行查询，检查数据是否不存在
            TeacherStandardVO teacher = teacherMapper.getStandardById(id);
            // 是：数据不存在，抛出异常
            if (teacher == null) {
                String message = "删除老师失败，尝试删除的数据不存在！";
                log.warn(message);
                throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
            }
        }
        log.debug("即将执行删除，参数：{}", id);
        int rows = teacherMapper.deleteById(id);
        if (rows != 1) {
            String message = "删除老师失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }
    }

    @Override
    public void updateInfoById(Long id, TeacherUpdateDTO teacherUpdateDTO) {
        log.debug("开始处理【修改老师详情】的业务，参数ID：{}，新数据：{}", id, teacherUpdateDTO);
        {
            // 调用Mapper的getStandardById()执行查询，检查数据是否不存在
            TeacherStandardVO teacher = teacherMapper.getStandardById(id);
            // 是：数据不存在，抛出异常
            if (teacher == null) {
                String message = "修改老师详情失败，尝试访问的数据不存在！";
                log.warn(message);
                throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
            }
        }
        {
            // 调用Mapper的countByName()执行查询，检查新的名称是否已经被别的数据占用
            int count = teacherMapper.countByName(teacherUpdateDTO.getName());
            // 是：名称被别的数据占用，抛出异常（NOT_CONFLICT）
            if (count > 0) {
                String message = "修改老师详情失败，新的老师名称已经被占用！";
                log.warn(message);
                throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
            }
        }
        // 创建Album对象
        Teacher teacher = new Teacher();
        // 将参数对象的属性值复制到以上新创建的Album对象中
        BeanUtils.copyProperties(teacherUpdateDTO, teacher);
        teacher.setId(id);
        // 调用Mapper的update(Album album)方法执行修改
        log.debug("即将执行修改数据，参数：{}", teacher);
        int rows = teacherMapper.update(teacher);
        if (rows != 1) {
            String message = "修改老师详情失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
    }

    @Override
    public TeacherStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据id查询老师详情】的业务，参数：{}", id);
        TeacherStandardVO teacher = teacherMapper.getStandardById(id);
        if (teacher == null) {
            String message = "查询老师详情失败，尝试访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        log.debug("即将返回查询结果：{}", teacher);
        return teacher;
    }

    @Override
    public List<TeacherListItemVO> list() {
        log.debug("开始处理【查询老师列表】的业务，无参数");
        List<TeacherListItemVO> list = teacherMapper.list();
        log.debug("即将返回查询结果：{}", list);
        return list;
    }
}
